//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssProductSaleDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.PssProductSale;
import com.aidong.model.wills.PssProductSaleList;
import com.aidong.model.wills.PtContract;
import com.aidong.model.wills.PtPackage;
import com.aidong.model.wills.PtPackageItem;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository
public class PtPackageDao extends BaseDao<PtPackage, String> {
    @Resource
    PtContractDao ptContractDao;
    @Resource
    PssProductSaleDao pssProductSaleDao;
    @Resource
    PssProductStockDao pssProductStockDao;

    public PtPackageDao() {
    }

    public List<PtPackage> getShopPackageList(String shopCode) {
        String sql = "select code, chName, totalPrice from PtPackage ic inner join PtPackageShop s on ic.code=s.packageCode\nwhere shopCode=:p1 and status=1 and ActiveTime<getdate() and InactiveTime>getdate()";
        return this.queryListBySql(PtPackage.class, sql, new Object[]{shopCode});
    }

    public List<DicShopCode> getPtPackageShopList(String code, boolean ball) {
        String sql;
        if(ball) {
            sql = "select s.code, s.chName, (case when a.shopCode is null then 0 else 1 end) status from DicShopCode s\nleft join PtPackageShop a on s.code=a.shopCode and packageCode = :p1\nwhere status>0 order by s.code";
        } else {
            sql = "select s.code, s.chName, status from DicShopCode s\ninner join PtPackageShop a on s.code=a.shopCode\nwhere packageCode = :p1 and status>0 order by s.code";
        }

        return this.queryListBySql(DicShopCode.class, sql, new Object[]{code});
    }

    public void addPtPackage(PtPackage ptPackage, List<PtPackageItem> itemList, List<String> shopList) {
        this.add(ptPackage);
        this.saveItemShop(ptPackage.getCode(), itemList, shopList);
    }

    public void updatePtPackage(PtPackage ptPackage, List<PtPackageItem> itemList, List<String> shopList) {
        this.update(ptPackage);
        this.saveItemShop(ptPackage.getCode(), itemList, shopList);
    }

    private void saveItemShop(String packageCode, List<PtPackageItem> itemList, List<String> shopList) {
        Iterator session = itemList.iterator();

        while(session.hasNext()) {
            PtPackageItem query = (PtPackageItem)session.next();
            if("-".equals(query.getPackageCode())) {
                PtPackageItem i$ = (PtPackageItem)this.getSession().get(PtPackageItem.class, query.getAutoId());
                if(i$ != null) {
                    this.getSession().delete(i$);
                }
            } else {
                query.setPackageCode(packageCode);
                this.getSession().saveOrUpdate(query);
            }
        }

        Session session1 = this.getSession();
        SQLQuery query1 = session1.createSQLQuery("delete from PtPackageShop where packageCode = :p1");
        query1.setString("p1", packageCode);
        query1.executeUpdate();
        query1 = session1.createSQLQuery("insert into PtPackageShop (packageCode, ShopCode) values (:p1, :p2)");
        query1.setString("p1", packageCode);
        Iterator i$1 = shopList.iterator();

        while(i$1.hasNext()) {
            String shopCode = (String)i$1.next();
            query1.setString("p2", shopCode);
            query1.executeUpdate();
        }

    }

    public boolean updateStatus(String code, int status) {
        String hql = "update PtPackage set status=:p1 where code=:p2";
        return this.updateByHql(hql, new Object[]{Integer.valueOf(status), code}) > 0;
    }

    public void addContract(PtContract contract, List<PayList> payList1, PssProductSale productSale, List<PssProductSaleList> productSaleList, List<PayList> payList2) {
        this.pssProductSaleDao.addSale(productSale, productSaleList, this.pssProductStockDao, payList2);
        this.ptContractDao.addContract(contract, payList1);
    }
}
